<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/8
 * Time: 14:54
 */

//官网demo 
$server = new swoole_websocket_server("0.0.0.0", 9501);

session_start();

$server->on('open', function ($server, $request) {
    //var_dump($request);
   // $server->push($request->fd, "hello, welcome\n");
});

$server->set(array(
    'worker_num' => 2,   //worker进程数
    'backlog' => 128,    //Listen队列长度
    'max_request' => 300, //worker进程的最大任务数
    'dispatch_mode'=> 2,  //数据包分发策略 1轮循模式 2固定模式 3抢占模式 4IP分配 5UID分配
    'task_ipc_mode' => 2,
    'daemonize' => 1,      //守护进程化
));

$server->on('Message', function($server, $frame) {
   $data =json_decode($frame->data,true);
   $array = (array)$data;
   $_SESSION['output']='';
   foreach ($array as $key => $value) {
    $queue = new SplQueue;
    //入队
    $queue->push($value);
    //出队
    $datas = $queue->shift();
    //查询队列中的排队数量
    $n = count($queue);
    var_dump($datas);
    echo $n;    
    // $splq = new SplQueue;
    // for($i = 0; $i < 1000000; $i++)
    // {
    //     $splq->push($value);

    //     if ($i % 100 == 99 and count($splq) > 100)
    //     {
    //         $popN = rand(10, 99);
    //         for ($j = 0; $j < $popN; $j++)
    //         {
    //           $datas=$splq->shift();
    //         }
    //     }
    // }

    // $popN = count($splq);
    // for ($j = 0; $j < $popN; $j++)
    // {
    //     $splq->pop();
    // }
   if(isset($datas["pushtime"])){
               
         $pushtime=(strtotime($datas['pushtime'])) * 1000;
         $time = explode ( " ", microtime () );  
         $time = $time [1] . ($time [0] * 1000);  
         $time2 = explode ( ".", $time );  
         $showtime = $time2 [0];
         $times=$pushtime-$showtime;
           swoole_timer_after($times, function() use ($datas){
            //echo '定时！';
            
            // $url = $value['ip'].':'.$value['port_number']."/api.php?op=collection";
            $url = $datas['url']."/api.php?op=collection";

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // 我们在POST数据哦！
            curl_setopt($ch, CURLOPT_POST, 1);
            // 把post的变量加上
            curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);

            $output = curl_exec($ch);

            curl_close($ch);

            $return=$_SESSION['output']=$_SESSION['output'].$output.",";

            var_dump($_SESSION);
          });

        } else {
            // echo '不定时！!';

            // $url = $value['ip'].':'.$value['port_number']."/api.php?op=collection"; 
            $url = $datas['url']."/api.php?op=collection";

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // 我们在POST数据哦！
            curl_setopt($ch, CURLOPT_POST, 1);
            // 把post的变量加上
            curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
            
            $output = curl_exec($ch);

            curl_close($ch);

            $return=$_SESSION['output']=$_SESSION['output'].$output.",";

             var_dump($_SESSION);
             


                
        }
  
} 
  $server->push($frame->fd,$return);
  unset($_SESSION['output']);  
});


$server->on('Close', function($server, $fd) {
    echo "connection close: ".$fd;
});



$server->start();

 
?>